<template>
	<view>
		<view class="product_item" v-for="(item,index) in productList">
			<view class="image">
				<image :src="item.image" mode="widthFix"></image>
			</view>
			<view class="info">
				<view class="title">
					{{item.store_name}}
				</view>
				<view class="tool">
					<view class="price">
						￥{{item.price}}
					</view>
					<view class="computed">
						<view class="computed_btn reduce" @click="reduce(item.id,cart[item.id])">
							-
						</view>
						<view class="computed_btn nums">
							{{cart[item.id]?cart[item.id]:0}}
						</view>
						<view class="computed_btn add" @click="add(item.id,item.price,item.store_name,item.cart_num)">
							+
						</view>
					</view>
				</view>

			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				productList: [],
				num: 0,
				params: {
					sid: 0,
					appointment_id: 0,
					uid: 0,
				},
				cart: []
			}
		},
		onLoad(options) {
			console.log(options, 'options')
			let {
				id,
				uid
			} = options
			this.params.appointment_id = options.appointment_id;
			this.params.sid = id;
			this.params.uid = uid;
			this.cart_list(this.params.sid, this.params.uid)
			this.getProductLsits()

		},
		methods: {
			getProductLsits() {
				const {
					id,
					uid,
					appointment_id
				} = this.params
				this.$u.api.getProducts(id, uid, appointment_id).then(res => {
					console.log(res, 'productList')

					this.productList = res.data
				})
			},
			add(product_id, price, product_name, cart_num) {
				console.log("aa");
				console.log(product_id);
				console.log(product_name);

				this.params.cart_num = 1;
				this.params.product_id = product_id;
				this.params.price = price;
				this.params.product_name = product_name;
				console.log(this.params);
				this.$u.api.cart_add(this.params).then(res => {
					console.log(res, 'u.api.cart_add');
					if (res.status == 200) {
						this.cart[product_id] = this.cart[product_id] + 1;
					}
				})
			},
			reduce(product_id, cart_num) {
				if (cart_num <= 0) {
					return false;
				}
				let params = {};
				params.appointment_id = this.params.appointment_id;
				params.uid = this.params.uid;
				params.product_id = product_id;
				params.cart_num = cart_num - 1;

				this.$u.api.cart_num(params).then(res => {
					console.log(res, 'cart_reduce');
					if (res.status == 200) {
						this.cart[product_id] = this.cart[product_id] - 1;
						getProductLsits()
					}
				})
			},
			del() {
				this.$u.api.cart_del({}).then(res => {
					console.log(res, 'cart_del');
					if (res.status == 200) {
						this.car_detail = res.data;
						getProductLsits()
					}
				})
			},
			cart_list(sid, uid) {
				console.log("啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊")
				console.log(sid, uid);
				let that = this;
				this.$u.api.cart_list({
					"sid": sid,
					"uid": uid,
					"appointment_id":that.params.appointment_id
				}).then(res => {
					console.log(res, 'cart_list');
					that.cart = res.data;
					getProductLsits()
				})
			}
		}
	}
</script>

<style lang="scss">
	.product_item {
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		padding: 12rpx 16rpx;

		.image {
			width: 400rpx !important;
			flex: 1;
			margin-right: 10rpx;
			image {
				width: 100%;
			}
		}

		.info {
			flex:3;
			.tool {
				display: flex;
				justify-content: space-between;
			}
		}
	}

	.computed {
		width: 220rpx;
		display: flex;
		justify-content: space-between;

		.computed_btn {

			width: 50rpx;
			height: 50rpx;
			line-height: 46rpx;
			border: 1px solid #999999;
			text-align: center;
			color: #666666;
			font-weight: 700;
			margin-left: 10rpx;

		}

		.nums {
			font-weight: normal;
		}
	}
</style>
